<?php

namespace App\Models\Api;
use App\Models\Api\Recode;
use App\Models\Api\Base;
use Cache;


class AwardGoods extends Base
{
    protected $table = 'award_goods';

    /**
     * 操作时间
     * @return string
     */
    protected function getFortuneTimeAttr($value, $data)
    {
        return date('Y-m-d', json_decode($data['extend'], true)['award_time']);
    }

    /**获取用户中奖的占星地图
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     * @throws \app\common\lib\exception\ApiException
     */
    public function get_award_acg($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];

        $where = ['auid' => $uid, 'agid' => ['in',config('goods.acg_id')]];
        $map = ['uid' => $uid, 'isself' => 1];

        //获取自己的档案信息
        $recode_info = (new Recode())->getSelfRecode($map);

        if ($recode_info['id'] == $rid)
        {
            $prize = false;

            if (intval($this->cache_on) >= 1 && Cache::has('award_acg_' . $uid))
            {
                $prize = Cache::get('award_acg_' . $uid);
            }

            if ($prize === false)
            {
                // 获取中奖标识
                try
                {
                    $prize = $this
                        ->select('agid','auid')
                        ->where($where)
                        ->get();

                    $prize = json_decode(json_encode($prize, true), true);
                }
                catch (\Exception $e)
                {
                    $prize = [];
                }
                if (intval($this->cache_on) >= 1)
                {
                    Cache::set('award_acg_' . $uid, $prize, 86400 * 7);
                }
            }
        } else
        {
            $prize = [];
        }
        return $prize;
    }
}